package whf.framework.security;

import java.util.List;
import java.util.Set;

import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.security.entity.Group;
import whf.framework.security.entity.Permission;
import whf.framework.security.entity.Role;
import whf.framework.security.entity.User;
import whf.framework.security.service.GroupService;
import whf.framework.security.service.PermissionService;
import whf.framework.security.service.RoleService;
import whf.framework.security.service.UserService;
import whf.framework.service.SpringService;
import whf.framework.util.BeanFactory;
import whf.framework.util.Utils;

/**
 * @author wanghaifeng
 *
 */
public class HibernateSpringUserManager extends SpringService implements UserManager {
	private static Log log = LogFactory.getLog(HibernateSpringUserManager.class);
	
	private GroupService groupService;
	
	private UserService userService;
	
	private RoleService roleService;
	
	private PermissionService permissionService;
	
	/**
	 * @modify wanghaifeng Aug 31, 2006 9:19:52 PM
	 * @param groupService
	 */
	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	/**
	 * @modify wanghaifeng Aug 31, 2006 9:19:55 PM
	 * @param userService
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * @param roleService The roleService to set.
	 */
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	/**
	 * @param permissionService The permissionService to set.
	 */
	public void setPermissionService(PermissionService permissionService) {
		this.permissionService = permissionService;
	}
	

	/**
	 * @modify wanghaifeng Oct 13, 2006 12:00:31 PM
	 * @return
	 */
	public static UserManager getUserManager(){
		return (UserManager)BeanFactory.getBean(HibernateSpringUserManager.class);
	}
	
	/* (non-Javadoc)
	 * @see whf.framework.auth.UserManager#canDo(whf.framework.auth.entity.User, java.lang.String, java.lang.String)
	 */
	public boolean canDo(User user, String target, String operation) {
		return false;
	}

	/* (non-Javadoc)
	 * @see whf.framework.auth.UserManager#findPermissions(whf.framework.auth.entity.User)
	 */
	public Set<Permission> findPermissions(User user) {
		Set<Permission> permissions = Utils.newHashSet();
		Set<Role> roles = this.findRoles(user);
		Set<Role> anRoles = this.findAnonymousRoles();
		roles.addAll(anRoles);
		for(Role r: roles){
			try{
				permissions.addAll(this.permissionService.findByRole(r));
			}catch(Exception e){
				log.error(this, e);
			}
		}
		return permissions;
	}

	/* (non-Javadoc)
	 * @see whf.framework.auth.UserManager#findRoles(whf.framework.auth.entity.User)
	 */
	public Set<Role> findRoles(User user) {
		Set<Role> roles = Utils.newHashSet();
		try{
			List<Group> groups = groupService.findByUser(user);
			for(Group g: groups){
				roles.addAll(g.getRoles());
			}
			Set<Role> rs = this.roleService.findByUser(user);
			roles.addAll(rs);
		}catch(Exception e){
			log.error(this, e);
		}
		return roles;
	}
	
	/* (non-Javadoc)
	 * @see whf.framework.auth.UserManager#findGroups(whf.framework.auth.entity.User)
	 */
	public Set<Group> findGroups(User user) {
		Set<Group> groups = Utils.newHashSet();
		try{
			List<Group> list = groupService.findByUser(user);
			groups.addAll(list);
		} catch(Exception e){
			log.error(this, e);
		}
		return groups;
	}

	/* (non-Javadoc)
	 * @see whf.framework.auth.UserManager#findUser(java.lang.String, java.lang.String)
	 */
	public User findUser(String username, String password) {
		try{
			return userService.findByUsernamePassword(username, password);
		}catch(Exception e){
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see whf.framework.auth.UserManager#hasPermission(whf.framework.auth.entity.User, whf.framework.auth.entity.Permission)
	 */
	public boolean hasPermission(User user, Permission perm) {
		Set<Permission> perms = this.findPermissions(user);
		return perms.contains(perm);
	}

	/* (non-Javadoc)
	 * @see whf.framework.auth.UserManager#findUserByUsername(java.lang.String)
	 */
	public User findUserByUsername(String username) {
		try{
			return this.userService.findByUsername(username);
		}catch(Exception e){
			log.info(this, e);
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see whf.framework.auth.UserManager#findAnonymousRoles()
	 */
	public Set<Role> findAnonymousRoles() {
		try{
			return this.roleService.findAnonymousRoles();
		}catch(Exception e){
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see whf.framework.auth.UserManager#findAnonymousGroups()
	 */
//	public Set<Group> findAnonymousGroups() {
//		try{
//			return this.groupService.findAnonymousGroups();
//		}catch(Exception e){
//			return null;
//		}
//	}
	
	public static void main(String[] args) throws Exception{
		UserManager manager = getUserManager();
		User user = manager.findUserByUsername("fm");
		log.debug(user);
		Set<Role> roles = manager.findRoles(user);
		log.debug(roles.size());
		Set<Permission> perms = manager.findPermissions(user);
		log.debug(perms);
		Set<Group> groups = manager.findGroups(user);
		log.debug(groups.size());
	}
}
